package com.cfr.restlet.servlet;

import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.cfr.restlet.server.WorkflowWebServiceManager;
import com.cfr.utils.CommonRepositoryConfiguration;


public class ServiceStarter extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		
		super.init();
		
		String log4jPath = this.getServletContext().getRealPath(".");
		System.setProperty("log.path", log4jPath);

		String pathString = ServiceStarter.class.getClassLoader().getResource("configfile").getPath().replace("%20", " ");
		CommonRepositoryConfiguration configuration = CommonRepositoryConfiguration.getInstance();
		configuration.init(pathString);

		WorkflowWebServiceManager workflowWebServiceManager = WorkflowWebServiceManager.getInstance();
		workflowWebServiceManager.startServer();
	}

	@Override
	public void destroy() {
		
		WorkflowWebServiceManager.getInstance().stopServer();

		Properties properties = System.getProperties();
		if (properties.containsKey("log.path")) {
			properties.remove("log.path");
		}
	}

}
